package org.example.bombermanclient.network;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.example.bombermanclient.model.Message;
import org.example.bombermanclient.util.JsonUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class JsonMessageEncoder extends MessageToMessageEncoder<Message> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
        String json = JsonUtils.toJson(msg) + "\n";
        System.out.println("Encoding message: " + json);  // 可打印调试

        ByteBuf buffer = Unpooled.copiedBuffer(json, StandardCharsets.UTF_8);
        out.add(buffer);
    }
}
